<?php

/**
 * This is the model class for table "sd.video".
 *
 * The followings are the available columns in table 'sd.video':
 * @property string $id
 * @property string $target_id
 * @property string $target_type
 * @property string $url
 * @property string $path
 * @property string $file
 * @property string $title
 * @property string $upload_time
 * @property string $code
 */
class Video extends CBaseActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.video';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('target_type', 'required'),
			array('target_id, upload_time', 'length', 'max'=>10),
			array('target_type', 'length', 'max'=>30),
			array('url, path, file', 'length', 'max'=>255),
			array('title, code', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, target_id, target_type, url, path, file, title, upload_time, code', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'target_id' => 'Target',
			'target_type' => 'Target Type',
			'url' => 'Url',
			'path' => 'Path',
			'file' => 'File',
			'title' => 'Title',
			'upload_time' => 'Upload Time',
			'code' => 'Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('target_id',$this->target_id,true);
		$criteria->compare('target_type',$this->target_type,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('path',$this->path,true);
		$criteria->compare('file',$this->file,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('upload_time',$this->upload_time,true);
		$criteria->compare('code',$this->code,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Video the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function selectByRow($oRow, $key = 'id')
	{
		if(!$oRow || empty($oRow))
			return false;

		$ids = array();
		$ids[] = $oRow->$key;
		$type = $oRow->getTableName();

		$oCriteria = $this->getDbCriteria();
		$oCriteria->addInCondition('target_id', $ids);
		$oCriteria->addColumnCondition(array('target_type' => $type));

		$oVideos = $this->findAll($oCriteria);

		$oRow->setVideos($oVideos);

		return $oVideos;
	}

	public function selectAllByRow(&$oTargetRow, $key = 'id')
	{
		if(!$oTargetRow)
			return $oTargetRow;

		if($oTargetRow->$key < 1)
			return $oTargetRow;

		$type = $oTargetRow->getTableName();

		$ids = array();
		$ids[] = $oTargetRow->$key;

		$oCriteria = $this->getDbCriteria();
		$oCriteria->addInCondition('target_id', $ids);
		$oCriteria->addColumnCondition(array('target_type' => $type));

		$oVideos = $this->findAll($oCriteria);

		$oTargetRow->setVideos($oVideos);

		return $oVideos;
	}

	public function saveByTarget(CBaseActiveRecord $oTargetRow, $data)
	{
		if(empty($data['videos']) || empty($oTargetRow))
			return false;

		foreach($data['videos'] as $i => $code)
		{
			$code = trim($code);
			if(empty($code))
				continue;

			$oRow = new self;

			$oRow->target_id = $oTargetRow->getPrimaryKey();
			$oRow->target_type = $oTargetRow->getTableName();
			$oRow->code = $code;
			$oRow->upload_time = time();

			$oRow->save();
		}

		return true;
	}

}
